package file;

import java.io.File;
import java.util.Scanner;

public class Demo11 {
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        System.out.println("请输入要搜索的目录：");
        String root= sc.next();
        File file=new File(root);
        if(!file.isDirectory()){
            System.out.println("输入的不是目录！");
            return;
        }else{
            System.out.println("请输入要删除的关键字：");
            String keyWord=sc.next();
            scanDir(file,keyWord);
        }
    }
    private static void scanDir(File file,String keyWord){
        File[] files=file.listFiles();
        if (files==null){
            return;
        }
        for (File file1: files) {
            if(file1.isFile()){
                dealFile(file1,keyWord);
            }else{
                scanDir(file1,keyWord);
            }
        }
    }
    private static void dealFile(File file,String keyWord){
        if (file.getName().contains(keyWord)){
            System.out.println("发现文件"+file.getAbsolutePath()+"包含关键字"+"是否删除(y/n)");
            Scanner sc=new Scanner(System.in);
            String s=sc.next();
            if (s.equalsIgnoreCase("y")){
                file.delete();
                System.out.println("文件已删除！");
            }
        }else{
            return;
        }
    }
}
